﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Belyn.Common.Web;

namespace CartoonViewer
{
    /// <summary>
    /// UC_DownloadItem.xaml 的交互逻辑
    /// </summary>
    public partial class UC_DownloadItem : UserControl
    {
        public UC_DownloadItem()
        {
            InitializeComponent();
        }
        public UC_DownloadListPanel DownloadListPanel;
        public DownloadState State;
        public UC_DownloadItem(Belyn.Common.Web.IDownload _Download):this()
        {
            this.Download = _Download;

            this.Download.Downloading += new Belyn.Common.Web.DownloadDelegate(Download_Downloading);
            this.Download.Canceled += new Belyn.Common.Web.DownloadDelegate(Download_Canceled);
            this.Download.Failed += new Belyn.Common.Web.DownloadFailedDelegate(Download_Failed);
            this.Download.SpeedChanged += new Belyn.Common.Web.DownloadDelegate(Download_SpeedChanged);
            this.Download.Suspended += new Belyn.Common.Web.DownloadDelegate(Download_Suspended); 
            this.Download.Started += new Belyn.Common.Web.DownloadDelegate(Download_Started);

            this.SetState(DownloadState.Stop);

            this.SetDownloadValue(this.Download);

        }

        public void SetDownloadValue(IDownload d)
        {
            this.FileName = d.FileName;
            this.Title = d.Title;
            this.Speed = d.Speed;
            this.Progress = d.Progress;
        }

        //根据当前下载状态来设置显示格式
        protected void SetState( DownloadState s )
        {
            this.State = s;
            ControlHelper.HideControl(this.BtnCancel);
            ControlHelper.HideControl(this.BtnStart);
            ControlHelper.HideControl(this.BtnSuspend);

            switch (this.State)
            { 
                case DownloadState.Downloading:
                    ControlHelper.ShowControl(this.BtnSuspend);
                    ControlHelper.ShowControl(this.BtnCancel);
                    break;
                case DownloadState.Stop:
                    ControlHelper.ShowControl(this.BtnStart);
                    break;
                case DownloadState.Success:
                    break;
                case DownloadState.Suspend:
                    ControlHelper.ShowControl(this.BtnStart);
                    ControlHelper.ShowControl(this.BtnCancel);
                    ControlHelper.SetValue(this.TxtSpeed, new Action(() =>
                    {
                        this.TxtSpeed.Text = " - - ";
                    })); 


                    break;
                default:
                    break;

            }
        }

        void Download_Started( Belyn.Common.Web.IDownload d )
        {
            this.SetState( DownloadState.Downloading );
        }
         
        void Download_Suspended(Belyn.Common.Web.IDownload d)
        {
            this.SetState( DownloadState.Suspend );
        }

        void Download_SpeedChanged(Belyn.Common.Web.IDownload d)
        {
            
            this.Progress = d.Progress;
            this.Speed = d.Speed;
            this.FileName = d.FileName;

        }

        void Download_Failed(Belyn.Common.Web.IDownload d, string message)
        {
            this.SetState( DownloadState.Stop );
            this.DownloadListPanel.RemoveItem(this);
        }

        void Download_Canceled(Belyn.Common.Web.IDownload d)
        {
            this.SetState( DownloadState.Stop );
            this.DownloadListPanel.RemoveItem(this);
        }

        void Download_Downloading(Belyn.Common.Web.IDownload d)
        {
            //this.SetState(DownloadState.Downloading);

        }

        protected Belyn.Common.Web.IDownload Download;

        protected double _Speed;
        public double Speed
        {
            get { return this._Speed; }
            set 
            {
                this._Speed = value;
                ControlHelper.SetValue(this.TxtSpeed, new Action(() =>
                {
                    this.TxtSpeed.Text = this.GetSpeedString(this._Speed);
                })); 
            }
        }

        protected double _Progress;
        public double Progress
        {
            get { return this._Progress; }
            set 
            { 
                this._Progress = value; 
                ControlHelper.SetValue(this.TxtProgress,new Action(()=>{
                    this.TxtProgress.Text = this.GetProgressString(this._Progress); 
                }));

                ControlHelper.SetValue(this.progressBar1, new Action(() =>
                {
                    this.progressBar1.Value = this._Progress; 
                })); 
            }
        }

         
        protected string _Title;
        public string Title
        {
            get { return this._Title; }
            set 
            { 
                this._Title = value;
                ControlHelper.SetValue(this.TxtTitle, new Action(() =>
                {
                    this.TxtTitle.Text = this._Title;
                }));
                
            }
        } 

        protected string _FileName;
        public string FileName
        {
            get { return this._FileName; }
            set 
            { 
                this._FileName = value;
                ControlHelper.SetValue(this.TxtFileName, new Action(() =>
                {
                    this.TxtFileName.Text = this._FileName; 
                }));
               
            }
        }

        protected string GetProgressString(double prg)
        {
            return prg > 0 ? (prg * 100).ToString("#.##") + "%" : "0%";
        }

        protected string GetSpeedString(double spd)
        {

            if (spd > 1048576)
            {
                return (spd / 1048576).ToString("#.##") + "MB/s";
            }
            else if (spd > 1024)
            {
                return (spd / 1024).ToString("#.##") + "KB/s";
            }

            return spd + "B/s"; 

        }

        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {

        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        protected bool _IsSelected = false;
        public bool IsSelected
        {
            get { return this._IsSelected; }
        }

        /// <summary>
        /// 设置选中状态 
        /// 
        /// </summary>
        /// <param name="select"></param> 
        public void SetSelected(bool select)
        {
            this._IsSelected = select;
            if (select)
            {
                this.Background = Brushes.AliceBlue;
            }
            else
            {
                this.Background = Brushes.Transparent;
            }
        }

     

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.BtnStart.Click += new RoutedEventHandler(BtnStart_Click);
            this.BtnSuspend.Click += new RoutedEventHandler(BtnSuspend_Click);
            this.BtnCancel.Click += new RoutedEventHandler(BtnCancel_Click);
        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            this.Download.Download();
            this.SetState(DownloadState.Downloading);
        }

        private void BtnSuspend_Click(object sender, RoutedEventArgs e)
        {
            this.Download.Suspend(); 
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Cancel();
        }

        public void Start()
        {
            this.SetState(DownloadState.Downloading);
            this.Download.Download(); 
        }
        public void Suspend()
        {
            this.Download.Suspend(); 
        }
        public void Cancel() 
        {
            this.Download.Cancel();
        }
        

        
    }
}
